#!/bin/bash

# 2007-2022 by Paul Sherman
# last modified Saturday, 04/09/2022
# for Absolute Linux.
# tests tgz and txz files to check if they are Slackware install packages,
# and sends thenm off to "installpkg" if they are.
# Makes use of .desktop file so we can right-click on packages adn choose to install them.
# GPL3, naturally :-)


if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
	dialog="Xdialog"
else
	dialog="dialog"
fi

if [ ! -e "$1" ]; then
	$dialog --title "Not Found" --msgbox "\n$1\nFile not found\n" 0 0
	exit
fi

fext=${1##*.}

if [ $fext != "tgz" ] && [ $fext != "txz" ]; then
	$dialog --title "Not a Package?" --msgbox "\n$1\nDoes not appear to be a valid software package\n" 0 0
	exit
fi

# make sure it IS a slackware installer package:
ret=`tar -tf "$1" | grep install/slack-desc`
if [ ! $ret ]; then
	filename="${1##*/}"
	$dialog --title "Slackware Package?" \
			--infobox "\n$filename\n\ndoes not apear to be\n  a Slackware Installer Package.  \n\n" 0 0 5000
	exit 0
fi  

# Get the proper package name
PKGNAM=`echo "${1##*/}" | sed 's/-[0-9].*//'`
NEWPKG=`echo "${1##*/}" | rev | cut -d"." -f2- | rev`
CWD=${1%/*}
PKGWHOLENAME=`echo "${1##*/}"`

# check if multiple versions are already installed...
ALREADYINSTALLED=$(ls -l /var/lib/pkgtools/packages | grep -w "$PKGNAM" | awk '{print $NF}' | sed 's/-[0-9].*//')
INNUM=0
IFS=$'\n' # make newlines the only separator
for j in $ALREADYINSTALLED
do
    if [ "$PKGNAM" == "$j" ]; then
        INNUM=$[$INNUM +1]
    fi
done

# full package name for reference when updating
OLDPKG=$(ls -l /var/lib/pkgtools/packages | grep -w "$PKGNAM" | awk '{print $NF}')
xPKG=""
IFS=$'\n' # make newlines the only separator
for j in $OLDPKG
do
    x=`echo $j | rev | cut -d '-' --complement -s -f1-3| rev`
    if [[ "$x" == $PKGNAM ]]; then
        xPKG="$j"
    fi
done

# not already installed, so we can call 
# installpkg, and run it in a term so folks can see what's doing...
if [ $INNUM -eq 0 ]; then
	if [[ $EUID -ne 0 ]]; then
	  cd $CWD
	  ktsuss xterm -e installpkg --verbose "$PKGWHOLENAME"
	else
	  cd $CWD
	  xterm -e installpkg --verbose "$PKGWHOLENAME"
	fi

	if [ $? = 1 ]; then
		clear
		$dialog --title "Error..." \
				--msgbox "$1\nwas not properly installed.\n\n" 0 0
	else
		$dialog --title "Complete" \
				--infobox "$1\nhas been installed.\n" 0 0 4000
	fi
	exit 0
fi


if [ $INNUM -gt 1 ]; then
	$dialog --title "Multiple Packages!" --msgbox "\nMultipe versions of this package already seem to be installed.\n\nIt is highly recommended that you remove all \nprevious versions before installing this package.\n" 0 0
	exit 0
fi

# if previous version is installed, we try to upgradepkg...
if [ $INNUM -eq 1 ]; then
        
    if [[ "$xPKG" == "$NEWPKG" ]]; then
        $dialog --title "Already Installed" --infobox "\n$xPKG\n\nis already installed.\n" 12 60 5000
        exit 0
    fi
	zenity --question --title="Update Current Package?" --width=400 --text "\n$xPKG\n is currently installed\n\nUpgrade to \n$NEWPKG?\n"
	if [ $? -eq 0 ]; then
		if [ $EUID -ne 0 ]; then
		  cd $CWD
		  ktsuss xterm -e upgradepkg --install-new --reinstall --verbose "$PKGWHOLENAME"
		else
		  cd $CWD
		  xterm -e upgradepkg --install-new --reinstall --verbose "$PKGWHOLENAME"
		  $dialog --title "Success" --infobox "\n$xPKG\n\nhas been upgraded.\n" 12 40 4000
		fi	
	else
        $dialog --title "Install Canceled" --infobox "\nGoodbye\n" 12 40 4000 
		exit 0
	fi
fi
exit 0
